# To change this template, choose Tools | Templates
# and open the template in the editor.

require 'rubygems'
require 'position_range'
require 'difflcs'

class BrainDiff

  def self.word_diff string_old, string_new

    diff = DiffLCS.longest_common_sub_strings(string_old.chars.to_a, string_new.chars.to_a)

    for i in 0..diff[:matched_old].length

      if diff[:matched_old][i] == diff[:matched_new][i]
        next
      end

      length_old = string_old.length
      length_new = string_new.length

      if diff[:matched_old][i].first - length_old == diff[:matched_new][i].first - length_new &&
          diff[:matched_old][i].last - length_old == diff[:matched_new][i].last - length_new
        next
      end

      diff[:matched_old].delete! diff[:matched_old][i]
      diff[:matched_new].delete! diff[:matched_new][i]
      i += -1
    end
    diff
  end

end